package com.liferay.portlet.login.action;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletRequest;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import com.hellodoctor.slayer.model.Profile;
import com.hellodoctor.slayer.model.impl.ProfileImpl;
import com.hellodoctor.slayer.service.ProfileLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.struts.BaseStrutsPortletAction;
import com.liferay.portal.kernel.struts.StrutsPortletAction;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.User;
import com.liferay.portal.security.auth.CompanyThreadLocal;
import com.liferay.portal.service.RoleLocalServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.util.PortalUtil;

public class CreateAccountAction2  extends BaseStrutsPortletAction  {
	public void processAction(StrutsPortletAction originalStrutsPortletAction,
			PortletConfig portletConfig, ActionRequest actionRequest,
			ActionResponse actionResponse) throws Exception {
		System.out.println("+++++++++++++++++++++++++BEFORE++++++++++++++++++++++++++++++++++++++++++++++++");	
		
		originalStrutsPortletAction.processAction(
			originalStrutsPortletAction, portletConfig, actionRequest, actionResponse);
		
		System.out.println("BOolean--------------->"+ParamUtil.getBoolean(actionRequest, "isDoctor")+"----"+RoleLocalServiceUtil.getRole(CompanyThreadLocal.getCompanyId(), "Doctor").getRoleId());
		assignRole(actionRequest);
/*		profileQuickAdd(actionRequest);
		
		checkInvitationStatus(actionRequest);*/
	}

	/**
	 * 
	 * @param actionRequest
	 */
	private void assignRole(PortletRequest portletRequest){
		System.out.println("Inside Assign role--------");
		boolean isDoctor = ParamUtil.getBoolean(portletRequest, "isDoctor",false);
		System.out.println("USER IS DOCTOR?-"+isDoctor);
		String emailAddress = ParamUtil.getString(portletRequest, "emailAddress");
		User user = null;
		//long userId = 0l;
		long[] roles = null;
		long roleId=0l;
		String roleName = isDoctor ? "Doctor" : "Patient" ;
		System.out.println("RoleNAme---"+roleName);
	
			try {
				roleId = RoleLocalServiceUtil.getRole(CompanyThreadLocal.getCompanyId(), roleName).getRoleId();
				System.out.println("RoleID of "+roleName+" is-"+roleId);
			} catch (PortalException e1) {
				e1.printStackTrace();
			} catch (SystemException e1) {
				e1.printStackTrace();
			}
		
		try {
		 user= UserLocalServiceUtil.getUserByEmailAddress(PortalUtil.getCompanyId(portletRequest), emailAddress);
		 System.out.println("UserName-"+user.getFullName());
		} catch (PortalException e) {
			e.printStackTrace();
		} catch (SystemException e) {
			e.printStackTrace();
		}
		try {
			 roles = user.getRoleIds();
			 System.out.println("ROLES for "+user.getFullName()+" are-"+roles);
		} catch (SystemException e) {
			e.printStackTrace();
		}
		if(Validator.isNotNull(user)){
			boolean roleIsPresentAlready = false;
			for(int i=0;i<=roles.length-1;i++){
				if(roles[i]==roleId) {
					System.out.println("roleIsPresent already!!!!");	
					roleIsPresentAlready = true;
					break;
				}
			}
			if(!roleIsPresentAlready){
				try {
					System.out.println("Role is not assigned...so assign now!");
					UserLocalServiceUtil.addRoleUser(roleId, user.getUserId());
					System.out.println("Role added to user");
				} catch (SystemException e) {
					e.printStackTrace();
				}
			}
			
		}
		Profile profile = new ProfileImpl(); 
		if(isDoctor){
		profile = ProfileLocalServiceUtil.createProfile(user.getUserId(), isDoctor);
		}
/*		RoleLocalServiceUtil.getRole(companyId, name)
		RoleLocalServiceUtil.addUserRole(user.getUserId(), roleId);
		 UserLocalServiceUtil.setRoleUsers(roleId, userIds);*/
		
	}
/*	private void checkInvitationStatus(PortletRequest portletRequest) {
		// check for invitation Id in the request
		long invitationId = ParamUtil.getLong(portletRequest, "invitationId", 0l);
		long inviterId = ParamUtil.getLong(portletRequest, "inviterId", 0l);
		
		String emailAddress = ParamUtil.getString(portletRequest, "emailAddress");
		if (invitationId > 0l) {
			InvitationLocalServiceUtil.updateInviation(invitationId, emailAddress);
		} else if (inviterId > 0l) {
			InvitationLocalServiceUtil.initInvitation(inviterId, emailAddress, 
					InviteConstants.STATUS_JOINED);
		}
	}
*/
	/**
	 * 
	 * @param actionRequest
	 */
/*	private void profileQuickAdd(PortletRequest portletRequest) {
				
		// Don't do anything if not custom registration
		boolean customRegistration = ParamUtil.getBoolean(portletRequest, "customRegistration", false);
		
		if (!customRegistration) return;

		String emailAddress = ParamUtil.getString(portletRequest, "emailAddress");
		String profileName = ParamUtil.getString(portletRequest, "profileName");
		
		boolean bride = ParamUtil.getBoolean(portletRequest, "bride");
		boolean creatingForSelf = ParamUtil.getBoolean(portletRequest, "creatingForSelf", false);

		ServiceContext serviceContext = null;
		try {
			serviceContext = ServiceContextFactory.getInstance(portletRequest);
		} catch (PortalException e) {
			e.printStackTrace();
		} catch (SystemException e) {
			e.printStackTrace();
		}
		
		ProfileLocalServiceUtil.init(bride, emailAddress, profileName, creatingForSelf, serviceContext);
	}
*/
	public String render(StrutsPortletAction originalStrutsPortletAction,
			PortletConfig portletConfig, RenderRequest renderRequest,
			RenderResponse renderResponse) throws Exception {
		
		return originalStrutsPortletAction.render(
			originalStrutsPortletAction, portletConfig, renderRequest, renderResponse);
	}
}